

%% load panel data
load('../../baseline_model/data_panel.mat')
load('../../baseline_model/data.mat')


%% aggregate data by decade with initial-valued covariates

countries=unique(data.cown);

data=data_panel(1:5*length(countries),[1:4,6:end]);
for t=1:5
    for n=1:length(countries)
        dn=data_panel(data_panel.cown==countries(n)&...
            data_panel.year>=1950+10*(t-1)&data_panel.year<1950+10*t,:);
        nt=find(isfinite(dn.terrain)&isfinite(dn.gdp_pc)&isfinite(dn.polity)&isfinite(dn.pop)&...
            isfinite(dn.USally)&isfinite(dn.UScolony)&isfinite(dn.USwar),1);
        if isempty(nt)
            nt=1;
        end
        data(length(countries)*(t-1)+n,1:4)=dn(1,1:4);
        data(length(countries)*(t-1)+n,5:end)=dn(nt,6:end);
    end
end

outcomes=outcomes_panel(1:5*length(countries),[1:4,5:end]);
for t=1:5
    for n=1:length(countries)
        dn=outcomes_panel(outcomes_panel.cown==countries(n)&...
            outcomes_panel.year>=1950+10*(t-1)&outcomes_panel.year<1950+10*t,:);
        outcomes(length(countries)*(t-1)+n,1:4)=dn(1,1:4);
        outcomes(length(countries)*(t-1)+n,5:end)=array2table(1*(sum(table2array(dn(:,5:end)))>0));
    end
end

nmissing=~isfinite(data.gdp_pc)|~isfinite(data.pop)...
    |~isfinite(data.polity)|~isfinite(data.USally);
nmissing=~nmissing;


%%
clearvars -except data outcomes nmissing
save('data_decade_initial.mat')

